<?php
namespace Question\Controller;
use Common\Controller\Base;
class IndexController extends Base {
    private $Question = NULL;
    private $Doubt = NULL;

    protected function _initialize() {
        parent::_initialize();
        $this->Question = D('Question/Question');
        $this->Doubt = D('Question/Doubt');
    }
    public function index() {
        //条件
        $where['status'] = array('eq','1');
        $where['is_back'] = array('eq','1');
        $count = $this->Question->where($where)->count();
        $page = $this->page($count, 5);
        $data = $this->Question->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array("id" => "DESC"))->select();
        foreach($data as &$val){
            switch($val['kemu']){
                case '1':
                    $val['kemu']="物理";
                    break;
                case '2':
                    $val['kemu']="化学";
                    break;
                case '3':
                    $val['kemu']="语文";
                    break;
                case '4':
                    $val['kemu']="数学";
                    break;
                case '5':
                    $val['kemu']="生物";
                    break;
                case '6':
                   $val['kemu']="地理";
                    break;
                case '7':
                    $val['kemu']="政治";
                    break;
                case '8':
                    $val['kemu']="历史";
                    break;
                default :
                   $val['kemu']="未知科目";
            }
        }
        $this->assign('list', $data);
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->display();
    }

    public function answer(){
        if (IS_POST) {
            $post = I('post.', NULL, 'htmlspecialchars');
            if ($this->Doubt->addCandidates($post)) {
                $this->success('你的疑问提交成功！正在等待审核后回复！', U('Index/index'));
            } else {
                $error = $this->Doubt->getError();
                $this->error($error ? $error : '提交失败！');
            }
        }else{
            $seo = seo('', "名师答疑", '师答疑关键词', '师答疑关描述');
            $this->assign('SEO', $seo);
            $this->display();
        }
    }

}
